package com.hymjweb.demo.hymj.finance_manage.invoice_register.controller;

import com.hymjweb.demo.framework.result.BaseResultMap;
import com.hymjweb.demo.framework.sys.BaseController;
import com.hymjweb.demo.framework.sys.SysConstant;
import com.hymjweb.demo.framework.util.DataTypeUtil;
import com.hymjweb.demo.hymj.finance_manage.invoice_register.bean.CwInvoiceRegisterBean;
import com.hymjweb.demo.hymj.finance_manage.invoice_register.service.CwInvoiceRegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/pages/finance_manage/invoice_register")
public class CwInvoiceRegisterController extends BaseController {

    @Autowired
    private CwInvoiceRegisterService cwInvoiceRegisterService;


    @RequestMapping("/invoiceRegisterAdd")
    public String invoiceRegisterAdd() {
        return "pages/finance_manage/invoice_register/invoiceRegisterAdd";
    }


    @RequestMapping("/invoiceRegisterList")
    public String invoiceRegisterList() {
        return "pages/finance_manage/invoice_register/invoiceRegisterList";
    }

    @RequestMapping("/queryInvoiceDetailList")
    @ResponseBody
    public Object queryInvoiceDetailList(@RequestParam(required = true) Map param) throws Exception {
        return cwInvoiceRegisterService.queryInvoiceDetailPageList(param);

    }

    @RequestMapping("/queryExcludeBill")
    @ResponseBody
    public Object queryExcludeBill(@RequestBody Map param) throws Exception {

        if (param == null && param.isEmpty()) {
            throwException("发票查询参数错误!");
        }

        String deliveryCustomerId = DataTypeUtil.toStringObject(param.get("deliveryCustomerId"));

        BaseResultMap baseResultMap = new BaseResultMap();

        if(DataTypeUtil.isEmptyStr(deliveryCustomerId)){
            baseResultMap.setOk(false);
            baseResultMap.setMessage("根据发货客户查询客户发票参数错误!");
            return baseResultMap;
        }


        List bNativeBillPojoList = cwInvoiceRegisterService.queryExcludeBillByCustomerId(deliveryCustomerId);

        if (bNativeBillPojoList == null || bNativeBillPojoList.isEmpty()) {
            baseResultMap.setOk(false);
            baseResultMap.setMessage("根据发货客户未查询到发票数据!");
            return baseResultMap;
        }

        baseResultMap.setOk(true);
        baseResultMap.setData(bNativeBillPojoList);

        return baseResultMap;
    }


    @RequestMapping("/save")
    @ResponseBody
    public Object save(@RequestBody CwInvoiceRegisterBean bean) throws Exception {

        if (bean == null) {
            throwException("参数错误!");
        }


        int affectRows = cwInvoiceRegisterService.save(bean);

        BaseResultMap baseResultMap = new BaseResultMap();
        baseResultMap.setOk(true);
        baseResultMap.setMessage("保存发票成功!");

        if (affectRows <= SysConstant.INT_ZERO) {
            baseResultMap.setOk(false);
            baseResultMap.setMessage("保存发票失败!");
        }

        return baseResultMap;
    }



}
